package com.goods.controller.business;

import com.goods.business.service.ConsumerService;
import com.goods.common.model.business.Consumer;
import com.goods.common.response.ResponseBean;
import com.goods.common.utils.ListPageUtils;
import com.goods.common.vo.business.ConsumerVO;
import com.goods.common.vo.system.PageVO;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "物资去处")
@RestController
@RequestMapping("business/consumer")
public class ConsumerController {

    @Resource
    private ConsumerService consumerService;
    //查询
    @GetMapping("/findConsumerList")
    public ResponseBean findOutStockList(Integer pageNum, Integer pageSize, Consumer consumer){
        List<ConsumerVO> list = consumerService.findConsumerList(consumer);
        List<ConsumerVO> page = ListPageUtils.page(list, pageSize, pageNum);
        return ResponseBean.success(new PageVO<>(list.size(),page));
    }
    //添加
    @PostMapping("/add")
    public ResponseBean addConsumer(@RequestBody Consumer consumer){
        Integer integer = consumerService.addConsumer(consumer);
        if (integer > 0){
            return ResponseBean.success();
        }else {
            return ResponseBean.error("添加失败惹");
        }
    }
    //回显
    @GetMapping("/edit/{id}")
    public ResponseBean edit(@PathVariable Long id){
        ConsumerVO consumerVO = consumerService.edit(id);
        return ResponseBean.success(consumerVO);
    }
    //修改
    @PutMapping("update/{id}")
    public ResponseBean update(@PathVariable Long id,
                               @RequestBody Consumer consumer){
        Integer integer = consumerService.update(id,consumer);
        if (integer > 0){
            return ResponseBean.success();
        }else {
            return ResponseBean.error("修改失败惹");
        }
    }
    //删除
    @DeleteMapping("/delete/{id}")
    public ResponseBean update(@PathVariable Long id){
        Integer integer = consumerService.delete(id);
        if (integer > 0){
            return ResponseBean.success();
        }else {
            return ResponseBean.error("删除失败惹");
        }
    }
    //查询所有
    @GetMapping("/findAll")
    private ResponseBean findAll(){
        List<ConsumerVO> list = consumerService.findAll();
        return ResponseBean.success(list);
    }
}
